package com.example.ywj.http.http;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.ywj.http.R;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;



import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GetActivity extends AppCompatActivity {

    private static final String TAG = "GetActivity";
    StringBuffer sb;
    @BindView(R.id.btn_http_get)
    public Button btn_http_get;
    @BindView(R.id.editText_username)
    public EditText et_username;
    @BindView(R.id.editText_password)
    public EditText et_password;
    //public String username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.btn_http_get})
    public void onClickView(View view){
        switch (view.getId()){
            case R.id.btn_http_get:
                func();
                break;
        }
    }
    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 0:
//                    //Toast.makeText(LoginActivity.this,msg.what,Toast.LENGTH_LONG).show();
//                    Intent intent = new Intent(LoginActivity.this,SuccessActivity.class);
//                    startActivity(intent);
//                    break;
//            }
        }
    };
    public void func(){
        new Thread(){
            @Override
            public void run() {
                String data = getData();
                Log.i(TAG, "run: "+data);
                //JSONObject obj = new JSONObject(data);
            }
        }.start();

    }
    public String getData(){
        try{
            String path = "http://10.14.249.189:3009/addUser?username="+et_username.getText().toString().trim()+"&password="+et_password.getText().toString().trim();
            //创建url对象
            URL url = new URL(path);
            //获得连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            conn.connect();
            int responseCode = conn.getResponseCode();
            if(responseCode == 200){
                //Log.i(TAG, "getData: "+"200");
                InputStream inputStream = conn.getInputStream();
                byte[] bytes = new byte[1024];
                int len = -1;
                sb = new StringBuffer();
                //读流
                while ((len = inputStream.read(bytes)) !=-1){
                    String str = new String(bytes,0,len);
                    sb.append(str);
                }
                JSONObject obj = new JSONObject(sb.toString());
                Log.i(TAG, "getData: "+obj);
                JSONObject data = obj.getJSONObject("data");
                String username = data.getString("username");
                String password = data.getString("password");
                //String username = obj.getString("username");
                //String password = obj.getString("password");
                Log.i(TAG, "getData: "+username);
                Log.i(TAG, "getData: "+password);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }
}
